// pages/order-detail/order-detail.js
var api = require('../../api.js');
var date =  require('../../utils/date.js');
var orderId = 0;

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgServer :  api.default.img_server,

        order: null,
        getGoodsTotalPrice: function () {
            return this.data.order.total_price;
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var page = this;
      orderId =options.id;
        page.setData({
          store:wx.getStorageSync("store")
        });
        wx.showLoading({
            title: "正在加载",
        });
        app.request({
            url: api.order.detail,
            data: {
                id: options.id,
            },
            success: function (res) {
              console.log(res)
                if (res.code == 200) {
                    page.setData({
                        order: res.data,
                    });
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        });
        app.request({
            url: api.order.getWayBillInfo,
            data: {
                orderId: options.id,
            },
            success: function (res) {
                if (res.code == 200) {
                    console.log(res.data);
                }
            },
            complete: function () {
                wx.hideLoading();
            }
        });
    },

    toPay:function(e){
      //获取支付数据
      app.request({
        url: api.order.pay_data,
        data: {
          id: orderId,
          payType: 2,
        },
        success: function (res) {
          console.log(res);
          //余额支付成功
          if (res.code == 201) {
            wx.showModal({
              title: "提示",
              content: "余额支付成功",
              showCancel: false,
              confirmText: "确认",
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: "/pages/order/order?status=1",
                  });
                }
              }
            });
          }
          //余额支付失败
          if (res.code == 202) {
            wx.showModal({
              title: "提示",
              content: "余额支付失败，请充值后购买",
              showCancel: false,
              confirmText: "确认",
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: "/pages/order/order?status=0",
                  });
                }
              }
            });
          }
          if (res.code == 200) {
            //发起支付
            wx.requestPayment({
              timeStamp: res.data.timeStamp,
              nonceStr: res.data.nonceStr,
              package: res.data.package,
              signType: res.data.signType,
              paySign: res.data.paySign,
              success: function (e) {
                console.log('1');
                console.log(e);
                wx.requestSubscribeMessage({
                  tmplIds: ['bPgXd4I-7E-GYafPJSNJEAQMuptkU_SYG1OscN7MM0o'],
                  success(res) {
                    console.log(res)
                  },
                  fail(error) {
                    console.log(error)
                  }
                });
                wx.redirectTo({
                  url: "/pages/order/order?status=1",
                });
              },
              fail: function (e) {
                console.log('2');
                console.log(e);
              },
              complete: function (e) {
                console.log('3');
                console.log(e);
                if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {//支付失败转到待支付订单列表
                  wx.showModal({
                    title: "提示",
                    content: "订单尚未支付",
                    showCancel: false,
                    confirmText: "确认",
                    success: function (res) {
                      if (res.confirm) {
                        wx.redirectTo({
                          url: "/pages/order/order?status=9",
                        });
                      }
                    }
                  });
                  return;
                }
                if (e.errMsg == "requestPayment:ok") {
                  return;
                }
                wx.redirectTo({
                  url: "/pages/order/order?status=1",
                });
              },
            });
            return;
          }
          if (res.code == 1) {
            wx.showToast({
              title: res.msg,
              image: "/images/icon-warning.png",
            });
            return;
          }
        }
      });
    }
,
  search: function (e) {
    //获取支付数据
    app.request({
      url: api.order.search,
      data: {
        com: "jd",
        num: "98489502125",
      },
      success: function (res) {
        console.log(res);
       
        if (res.code == 200) {
          
          return;
        }
        if (res.code == 1) {
          wx.showToast({
            title: res.msg,
            image: "/images/icon-warning.png",
          });
          return;
        }
      }
    });
  }
  ,
    copyText: function (e) {
        var page = this;
        var text = e.currentTarget.dataset.text;
        wx.setClipboardData({
            data: text,
            success: function () {
                wx.showToast({
                    title: "已复制"
                });
            }
        });
    }

});